package com.aishuang.user.web;

import com.aishuang.user.config.PatternProperties;
import com.aishuang.user.config.ServerConfig;
import com.aishuang.user.pojo.User;
import com.aishuang.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
// @RefreshScope  // 热更新 一旦nacos 配置文件修改了  不用重启就可以拿到最新数据
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ServerConfig serverConfig;

//    @Value("${pattern.dataformat}")
//    private String dataformat;

    @Autowired
    private PatternProperties patternProperties;
    @Value("${user.name01}")
    private String name01;

    @Value("${user.env}")
    private String env;

    @Value("${user.common}")
    private String common;
    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id, HttpServletRequest request) {
        String web44 = request.getHeader("web44");
        System.out.println(web44);
        String config = request.getHeader("config");
        System.out.println(config);
        Integer port = serverConfig.getPort();
        System.out.println(port);
        User user = userService.queryById(id);
        user.setPort(port);
        return user;
    }


    @GetMapping("now")
    public ResponseEntity<Map<String,Object>> queryNow() {
        LocalDateTime now = LocalDateTime.now();
        // DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dataformat);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(patternProperties.getDataformat());
        String dateString = formatter.format(now);
        String envShareValue = patternProperties.getEnvShareValue();
        Map<String,Object> map = new HashMap<>();
        map.put("dateString",dateString);
        map.put("envShareValue",envShareValue);
        return ResponseEntity.ok(map);
    }

    @GetMapping("name01")
    public ResponseEntity<Map<String,Object>> queryName() {

        Map<String,Object> map = new HashMap<>();
        map.put("name",name01);

        return ResponseEntity.ok(map);
    }

    @GetMapping("name02")
    public ResponseEntity<Map<String,Object>> queryName2() {

        Map<String,Object> map = new HashMap<>();
        map.put("env",env);
        map.put("common",common);
        return ResponseEntity.ok(map);
    }

}
